import DList from "@/components/dlist";
export default {
  data() {
    return {
      typeList: [{
        value: 1,
        name: "评价",
        sort: "",
        active: false
      },{
        value: 2,
        name: "销售",
        sort: "",
        active: false
      },{
        value: 3,
        name: "价格",
        sort: "",
        active: false
      }],
      permutation: true,
      address: {},
      search_title: "",
      goodsList: [],
      loading: false,
      finished: false,
      page: 1
    };
  },
  activated() {
    this.initData();
    this.initLocation();
  },
  methods: {
    initLocation() {
      this.fun
        .getLocation()
        .then(res => {
          this.address = res;
          this.getGoodsList();
        })
        .catch(err => {
          console.log(err);
        });
    },
    chooseTab(ob) {
      for(let item of this.typeList) {
        item.active = false;
        if(item.value == ob.value) {
          item.active = ob.sort == "desc" ? false : true;
          item.sort = ob.sort == "asc" ? "desc" : ob.sort == "desc" ? "" : "asc";
        } else {
          item.sort = ""
        }
      }
      this.initData();
      this.getGoodsList();
    },
    initData() {
      this.goodsList = [];
      this.loading = false;
      this.finished = false;
      this.page = 1;
    },
    async getGoodsList() {
      this.loading = true;
      let { data, result, msg } = await $http.post("plugin.supplier-local-life.frontend.index.get-goods-list", {
        adcode: this.address.addressComponent.adcode,
        category_id: "",
        page: this.page,
        search: {
          title: this.search_title
        },
        order_field: this.typeList.filter(item => { return item.active }).length ? this.typeList.filter(item => item.active).map(citem => {
          return citem.value
        })[0] : "",
        order_by: this.typeList.filter(item => { return item.active }).length ? this.typeList.filter(item => { return item.active }).map(item => {
          return item.sort
        })[0] : ""
      });
      if (result) {
        this.goodsList.push(...data.data);
        if (data.data.length === 0 || data.data.length < data.per_page || data.current_page === data.last_page) {
          this.finished = true;
        }
        this.page++;
        this.loading = false;
      } else {
        this.$toast(msg);
      }
    },
    onLoad() {
      this.getGoodsList();
    },
    searchData(event) {
      if (event.keyCode === 13) {
        //如果按的是enter键
        event.preventDefault();
        this.initData();
        this.getGoodsList();
      }
    },
    // 跳转商品详情
    goGoodsDetail(id) {
      this.$router.push(this.fun.getUrl("goods", { id }));
    },
  },
  components: { DList }
};
